<template>
  <div class="medicalConsortium pd-20">
    <el-button type="primary"
               @click="handleOption('add', null)">新 增</el-button>
    <element-table class="mt-20"
                   v-loading="loading"
                   :columns="columns"
                   :data="tableData"
                   pager
                   :pageTotal="pageTotal"
                   :pageIndex="pageIndex"
                   :pageSize="pageSize"
                   @handleChangePage="handleChangePage">
      <template v-slot:op="{row}">
        <el-button type="primary"
                   @click="handleOption('edit', row)">编 辑</el-button>
        <el-button type="danger"
                   @click="handleOption('del', row)">删 除</el-button>
      </template>
    </element-table>
    <element-dialog :visible.sync="isShow"
                    v-if="isShow"
                    :dataObj="dataObj"></element-dialog>
  </div>
</template>
<script>
  import elementTable from '@/base/ElementTable';
  import elementDialog from './elementDialog';
  export default {
    name: 'medicalConsortium', // 医联体管理
    data () {
      return {
        loading: false,
        isShow: false,
        tableData: [],
        pageTotal: 0,
        pageIndex: 1,
        pageSize: 20,
        columns: [
          {
            label: '医联体名称',
            prop: 'name',
            align: 'center'
          },
          {
            label: '头部医院',
            prop: 'hospitalName',
            align: 'center'
          },
          {
            label: '下属单位数量',
            prop: 'unionHospitalNum',
            align: 'center'
          },
          {
            label: '创建时间',
            prop: 'createTime',
            align: 'center',
            width: 150,
            formatter: (row, cellValue) => {
              return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
            }
          },
          {
            label: '操作',
            prop: 'op',
            width: 180,
            align: 'center',
            isSlot: true
          }
        ],
        dataObj: {}
      }
    },
    methods: {
      async getList () {
        const { data: res } = await this.post(this.api.medicalConsortium.getMedicalUnionPage, {
          pageIndex: this.pageIndex - 1,
          pageSize: this.pageSize
        })
        if (res.code === '200') {
          this.tableData = res.object;
          this.pageTotal = res.totalNum;
        }
      },
      // 分页
      handleChangePage (page) {
        this.pageIndex = page;
        this.getList();
      },
      handleOption (op, row) {
        switch (op) {
          case 'add':
            this.dataObj = null;
            this.isShow = true;
            break;
          case 'edit':
            this.isShow = true;
            this.dataObj = Object.assign({}, row);
            break;
          case 'del':
            this.$confirm('您是否要删除该医联体？')
              .then(async _ => {
                const { data: res } = await this.post(this.api.medicalConsortium.delMedicalUnion, {
                  id: row.id
                })
                if (res.code === '200') {
                  this.$message.success('删除成功!');
                  this.getList();
                }
              })
              .catch(_ => { });
            break;
        }
      }
    },
    created () {
      this.getList();
    },
    components: {
      'element-table': elementTable,
      'element-dialog': elementDialog
    }
  }
</script>
<style scoped lang="scss">
</style>